﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
using Hartley.Util;

namespace WebApplication1
{
    public partial class viewBestResults : System.Web.UI.Page
    {
        protected int Rows
        {
            get
            {
                return ViewState["Rows"] != null ? (int)ViewState["Rows"] : 0;
            }
            set
            {
                ViewState["Rows"] = value;
            }
        }

        // Columns property to hold the Columns in the ViewState
        protected int Columns
        {
            get
            {
                return ViewState["Columns"] != null ? (int)ViewState["Columns"] : 0;
            }
            set
            {
                ViewState["Columns"] = value;
            }
        }

      

        protected int currentPageNumber = 1;
        List<StudentDTO> bestStudentResults;
        string queryString;
        StudentAL studentAL;
        SportsUtil sportsUtil;
        const int MIN_NO_OF_As_FOR_BST_RSLT_FOR_AL = 1;
        const int MIN_NO_OF_As_FOR_BST_RSLT_FOR_OL = 4;
        const int MIN_NO_OF_TOTAL_PASS_FOR_BST_RSLT_FOR_AL = 3;
        const int MIN_NO_OF_TOTAL_PASS_FOR_BST_RSLT_FOR_OL = 6;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                queryString = Request.QueryString.ToString();

                string afterRemovedPlusSign = "";
                string selectedExamType = "";
                int selectedResultYr = 0;
                sportsUtil = new SportsUtil();


                string[] arguments = new string[5];
                arguments = queryString.Split('&');

                foreach (string argument in arguments)
                {
                    if (argument.Contains("+"))
                    {
                        afterRemovedPlusSign = argument.Replace("+", " ");
                    }
                    else
                    {
                        afterRemovedPlusSign = argument;
                    }



                    if (argument.Contains("SelectedExamType"))
                    {

                        selectedExamType = afterRemovedPlusSign.Substring((afterRemovedPlusSign.IndexOf('=') + 1));
                    }

                    if (argument.Contains("Year"))
                    {

                        selectedResultYr =Convert.ToInt32( sportsUtil.decryptQueryStrings( afterRemovedPlusSign.Substring((afterRemovedPlusSign.IndexOf('=') + 1))));
                    }

                   
                }

                 CreateDynamicTable(selectedResultYr, selectedExamType);

            }
        }

          private void CreateDynamicTable(int selectedResultYr, string selectedExamType)
          {
              PlaceHolder5.Controls.Clear();

              StudentAL studentAL = new StudentAL();
              
              List<ALResultsDTO> alBestResultDTO = new List<ALResultsDTO>();
              List<OLResultsDTO> olBestResultDTO = new List<OLResultsDTO>();
              if (selectedExamType.Equals("ALevel"))
              {
                  //Diplay all alevel results who got atleast one A & passed atleast 3 subjects
                 // alBestResultDTO = studentAL.GetALevelResultByYear(selectedResultYr).Where(a=>a.NoOfAs >=1).Where(b=>(b.NoOfAs + b.NoOfBs + b.NoOfCs) >=3).OrderByDescending(c=>c.NoOfAs).OrderByDescending(d=>d.NoOfBs).OrderByDescending(e=>e.NoOfCs).OrderByDescending(f=>f.NoOfSs).ToList();
                  alBestResultDTO = studentAL.GetALevelResultByYear(selectedResultYr).Where(a => a.NoOfAs >= MIN_NO_OF_As_FOR_BST_RSLT_FOR_AL).Where(b => (b.NoOfAs + b.NoOfBs + b.NoOfCs) >= MIN_NO_OF_TOTAL_PASS_FOR_BST_RSLT_FOR_AL).ToList();
                  alBestResultDTO = alBestResultDTO.OrderByDescending(f => f.NoOfFs).ToList();
                  alBestResultDTO = alBestResultDTO.OrderByDescending(f => f.NoOfSs).ToList();
                 alBestResultDTO = alBestResultDTO.OrderByDescending(e => e.NoOfCs).ToList();
                 alBestResultDTO = alBestResultDTO.OrderByDescending(d => d.NoOfBs).ToList();
                 alBestResultDTO = alBestResultDTO.OrderByDescending(c => c.NoOfAs).ToList();
              }
               if (selectedExamType.Equals("OLevel"))
              {
                   //Diplay all alevel results who got atleast one 4As & passed atleast 6 subjects
                  olBestResultDTO = studentAL.GetOLevelResultByYear(selectedResultYr).Where(a=>a.NoOfAs >= MIN_NO_OF_As_FOR_BST_RSLT_FOR_OL).Where(b=>(b.NoOfAs + b.NoOfBs + b.NoOfCs) >=MIN_NO_OF_TOTAL_PASS_FOR_BST_RSLT_FOR_OL).ToList();
                  olBestResultDTO = olBestResultDTO.OrderByDescending(c => c.NoOfFs).ToList();
                  olBestResultDTO = olBestResultDTO.OrderByDescending(d => d.NoOfSs).ToList();
                  olBestResultDTO = olBestResultDTO.OrderByDescending(e => e.NoOfCs).ToList();
                  olBestResultDTO = olBestResultDTO.OrderByDescending(f => f.NoOfBs).ToList();
                  olBestResultDTO = olBestResultDTO.OrderByDescending(g => g.NoOfAs).ToList();

              }

              //List<AthleticDataDTO> athleticDTO = athleticAL.GetCompetitionById(competnDataDTO[0].CompetionId);


              // Create a Table and set its properties 
              Table tbl = new Table();
              tbl.Width = new Unit("100%");
              tbl.BorderStyle = BorderStyle.Solid;
              // Add the table to the placeholder control
              PlaceHolder5.Controls.Add(tbl);


              Label lblResultYear = new Label();
              lblResultYear.Text = "Year";

              Label lblResultType = new Label();
              lblResultType.Text = "Exam Type";

              //Label lblResultStream = new Label();
              //lblResultStream.Text = "Stream";

              List<Label> allHeadeLabels = new List<Label>();

              allHeadeLabels.Add(lblResultYear);
              allHeadeLabels.Add(lblResultType);
              //allHeadeLabels.Add(lblResultStream);
              
              if(selectedExamType.Equals("ALevel"))
              {
                 int i = 0;
                foreach (Label lblHeader in allHeadeLabels)
                {


                  TableRow trHeader = new TableRow();
                  trHeader.Width = new Unit("50%");

                  TableCell tcHeader = new TableCell();
                  TableCell tcHeaderValue = new TableCell();

                  Label lblValue = new Label();
                  if (i % 2 == 0)
                  {
                      tcHeader.BackColor = System.Drawing.Color.Blue;
                      tcHeader.ForeColor = System.Drawing.Color.White;

                      tcHeaderValue.BackColor = System.Drawing.Color.Blue;
                      tcHeaderValue.ForeColor = System.Drawing.Color.White;

                  }
                  else
                  {
                      tcHeader.BackColor = System.Drawing.Color.White;
                      tcHeader.ForeColor = System.Drawing.Color.Blue;

                      tcHeaderValue.BackColor = System.Drawing.Color.White;
                      tcHeaderValue.ForeColor = System.Drawing.Color.Blue;
                  }

                  if (lblHeader.Text == "Year")
                  {
                      if (alBestResultDTO[0].Year != 9999)
                      {
                          lblValue.Text = alBestResultDTO[0].Year.ToString();
                      }
                      else
                      {
                          lblValue.Text = "N/A";
                      }
                  }

                  else if (lblHeader.Text == "Exam Type")
                  {
                       lblValue.Text = "G.C.E A/L";
                  }

                 /* else if (lblHeader.Text == "Stream")
                  {
                      lblValue.Text = alBestResultDTO[0].Stream.ToString();
                  }*/

                 
                  tcHeader.Controls.Add(lblHeader);
                  tcHeaderValue.Controls.Add(lblValue);
                  trHeader.Cells.Add(tcHeader);
                  trHeader.Cells.Add(tcHeaderValue);


                  tbl.Rows.Add(trHeader);
                  i++;
              }

              int tblRows = alBestResultDTO.Count;


              // Now iterate through the table and add your controls 
              for (int j = 0; j < tblRows; j++)
              {
                  List<StudentDTO> studentDTO = studentAL.GetStudentById(alBestResultDTO[j].StudentId);
                  TableRow trBody = new TableRow();
                  trBody.Width = new Unit("50%");

                  TableCell tcBodyStudentName = new TableCell();
                  TableCell tcBodyALResults = new TableCell();
                  TableCell tcBodyALStream = new TableCell();

                  Label lblALResults = new Label();
                  Label lblALStream = new Label();
                  HyperLink hlStudentName = new HyperLink();
                  if (i % 2 == 0)
                  {
                      tcBodyALResults.BackColor = System.Drawing.Color.DarkSlateBlue;
                      tcBodyALResults.ForeColor = System.Drawing.Color.White;

                      tcBodyStudentName.BackColor = System.Drawing.Color.DarkSlateBlue;
                      tcBodyStudentName.ForeColor = System.Drawing.Color.White;

                      //tcBodyALStream.BackColor = System.Drawing.Color.DarkSlateBlue;
                      //tcBodyALStream.ForeColor = System.Drawing.Color.White;

                  }
                  else
                  {
                      tcBodyALResults.BackColor = System.Drawing.Color.White;
                      tcBodyALResults.ForeColor = System.Drawing.Color.DarkSlateBlue;

                      tcBodyStudentName.BackColor = System.Drawing.Color.White;
                      tcBodyStudentName.ForeColor = System.Drawing.Color.DarkSlateBlue;

                      //tcBodyALStream.BackColor = System.Drawing.Color.DarkSlateBlue;
                      //tcBodyALStream.ForeColor = System.Drawing.Color.White;
                  }

                  string alrslt ="";
                  if(alBestResultDTO[j].NoOfAs!=0)
                  {
                      alrslt += alBestResultDTO[j].NoOfAs +"A";
                  }
                  if(alBestResultDTO[j].NoOfBs!=0)
                  {
                      alrslt += ", "+alBestResultDTO[j].NoOfBs +"B";
                  }
                  if(alBestResultDTO[j].NoOfCs!=0)
                  {
                      alrslt +=", "+ alBestResultDTO[j].NoOfCs +"C";
                  }
                  if(alBestResultDTO[j].NoOfSs!=0)
                  {
                      alrslt +=", "+ alBestResultDTO[j].NoOfAs +"S";
                  }
                  if(alBestResultDTO[j].NoOfFs!=0)
                  {
                      alrslt += ", "+alBestResultDTO[j].NoOfAs +"F";
                  }

                  lblALResults.Text = alrslt + " ( " + alBestResultDTO[j].Stream + " ) ";
                  //lblALStream.Text = alBestResultDTO[j].Stream;
                  hlStudentName.Text = studentDTO[0].FullName;
                  hlStudentName.NavigateUrl = "viewStudentById.aspx?" + studentDTO[0].StudentId;

                  tcBodyStudentName.Controls.Add(hlStudentName);
                  tcBodyALResults.Controls.Add(lblALStream);
                  tcBodyALResults.Controls.Add(lblALResults);
                  

                  trBody.Cells.Add(tcBodyALResults);
                  trBody.Cells.Add(tcBodyStudentName);


                  tbl.Rows.Add(trBody);
                  i++;
              }
              }
              else if(selectedExamType.Equals("OLevel"))
              {
              int i = 0;
               //if it's O/L thn no need to display stream

              //allHeadeLabels.Remove(lblResultStream);
              foreach (Label lblHeader in allHeadeLabels)
              {


                  TableRow trHeader = new TableRow();
                  trHeader.Width = new Unit("50%");

                  TableCell tcHeader = new TableCell();
                  TableCell tcHeaderValue = new TableCell();

                  Label lblValue = new Label();
                  if (i % 2 == 0)
                  {
                      tcHeader.BackColor = System.Drawing.Color.Blue;
                      tcHeader.ForeColor = System.Drawing.Color.White;

                      tcHeaderValue.BackColor = System.Drawing.Color.Blue;
                      tcHeaderValue.ForeColor = System.Drawing.Color.White;

                  }
                  else
                  {
                      tcHeader.BackColor = System.Drawing.Color.White;
                      tcHeader.ForeColor = System.Drawing.Color.Blue;

                      tcHeaderValue.BackColor = System.Drawing.Color.White;
                      tcHeaderValue.ForeColor = System.Drawing.Color.Blue;
                  }

                  if (lblHeader.Text == "Year")
                  {
                      if (olBestResultDTO[0].Year!=9999)
                      {
                      lblValue.Text = olBestResultDTO[0].Year.ToString();
                      }
                      else
                      {
                          lblValue.Text = "N/A";
                      }
                  }

                  else if (lblHeader.Text == "Exam Type")
                  {
                      lblValue.Text = "G.C.E O/L";
                  }

                 
                  tcHeader.Controls.Add(lblHeader);
                  tcHeaderValue.Controls.Add(lblValue);
                  trHeader.Cells.Add(tcHeader);
                  trHeader.Cells.Add(tcHeaderValue);


                  tbl.Rows.Add(trHeader);
                  i++;
              }

              int tblRows = olBestResultDTO.Count;
                
               

              // Now iterate through the table and add your controls 
              for (int j = 0; j < tblRows; j++)
              {
                  List<StudentDTO> studentDTO = studentAL.GetStudentById(olBestResultDTO[j].StudentId);
                  TableRow trBody = new TableRow();
                  trBody.Width = new Unit("50%");

                 
                  TableCell tcBodyStudentName = new TableCell();
                  TableCell tcBodyOLResults = new TableCell();
                  Label lblOLResults = new Label();
                  HyperLink hlStudentName = new HyperLink();
                  if (i % 2 == 0)
                  {
                      tcBodyOLResults.BackColor = System.Drawing.Color.DarkSlateBlue;
                      tcBodyOLResults.ForeColor = System.Drawing.Color.White;

                      tcBodyStudentName.BackColor = System.Drawing.Color.DarkSlateBlue;
                      tcBodyStudentName.ForeColor = System.Drawing.Color.White;

                  }
                  else
                  {
                      tcBodyOLResults.BackColor = System.Drawing.Color.White;
                      tcBodyOLResults.ForeColor = System.Drawing.Color.DarkSlateBlue;

                      tcBodyStudentName.BackColor = System.Drawing.Color.White;
                      tcBodyStudentName.ForeColor = System.Drawing.Color.DarkSlateBlue;
                  }

                  string olrslt = "";
                  if (olBestResultDTO[j].NoOfAs != 0)
                  {
                      olrslt += olBestResultDTO[j].NoOfAs + "A";
                  }
                  if (olBestResultDTO[j].NoOfBs != 0)
                  {
                      olrslt += ", " + olBestResultDTO[j].NoOfBs + "B";
                  }
                  if (olBestResultDTO[j].NoOfCs != 0)
                  {
                      olrslt += ", " + olBestResultDTO[j].NoOfCs + "C";
                  }
                  if (olBestResultDTO[j].NoOfSs != 0)
                  {
                      olrslt += ", " + olBestResultDTO[j].NoOfAs + "S";
                  }
                  if (olBestResultDTO[j].NoOfFs != 0)
                  {
                      olrslt += ", " + olBestResultDTO[j].NoOfAs + "F";
                  }
                  lblOLResults.Text = olrslt;
                  hlStudentName.Text = studentDTO[0].FullName;
                  hlStudentName.NavigateUrl = "viewStudentById.aspx?" + studentDTO[0].StudentId;


                  tcBodyOLResults.Controls.Add(lblOLResults);
                  tcBodyStudentName.Controls.Add(hlStudentName);

                  trBody.Cells.Add(tcBodyStudentName);
                  trBody.Cells.Add(tcBodyOLResults);
                  


                  tbl.Rows.Add(trBody);
                  i++;
              }
              }
             

              // Add the TableRow to the Table

              //tbl.Rows.Add(trBody);
              // }

              // This parameter helps determine in the LoadViewState event,
              // whether to recreate the dynamic controls or not

              ViewState["dynamictable"] = true;
          }
 
    }
}